package com.xd.clap.utils.convenient;

import cn.hutool.core.util.PhoneUtil;
import com.xd.clap.commons.SystemConstant;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author gxf
 * @date 2023/4/18 17:08
 */
public class IsPhoneValidation implements ConstraintValidator<IsPhone, Long> {

    @Override
    public void initialize(IsPhone constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(Long value, ConstraintValidatorContext constraintValidatorContext) {
        String phone = value.toString();
        //判定空+长度判定
        if (StringUtils.isEmpty(phone) || phone.length() != SystemConstant.NUM_ELEVEN) {
            return false;
        } else {
            return PhoneUtil.isMobile(phone);
        }
    }
}
